package com.fastwire.carriere.common.util;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.sql.rowset.serial.SerialException;

import org.tmforum.mtop.fmw.xsd.gen.v1.NameAndStringValueType;
import org.tmforum.mtop.fmw.xsd.nam.v1.NamingAttributeType;
import org.tmforum.mtop.fmw.xsd.nam.v1.RelativeDistinguishNameType;
import org.tmforum.mtop.nrb.xsd.lp.v1.LayeredParametersListType;
import org.tmforum.mtop.nrb.xsd.lp.v1.LayeredParametersType;

import com.fastwire.carriere.common.constant.HuaweiInventoryConstants;
import com.fastwire.carriere.inventory.entity.TransmissionParameter;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

// TODO: Auto-generated Javadoc
/**
 * The Class EntityUtil.
 */
public class EntityUtil {

	/**
	 * Convert rtn list to string.
	 * 
	 * @param rdnList
	 *            the rdn list
	 * @return the string
	 */
	public static String convertRtnListToString(
			List<RelativeDistinguishNameType> rdnList) {

		StringBuffer stringBuffer = new StringBuffer();

		for (RelativeDistinguishNameType rdnType : rdnList) {
			stringBuffer.append(rdnType.getType());
			stringBuffer.append("=");
			stringBuffer.append(rdnType.getValue());

			if (rdnList.indexOf(rdnType) != rdnList.size() - 1)
				stringBuffer.append("::");

		}

		return stringBuffer.toString();
	}

	/**
	 * Convert layered trans parameters.
	 * 
	 * @param layeredParametersListType
	 *            the layered parameters list type
	 * @return the list
	 */
	public static Set<TransmissionParameter> convertLayeredTransParameters(
			LayeredParametersListType layeredParametersListType) {
		if (layeredParametersListType == null) {
			return null;
		}
		List<LayeredParametersType> LayeredtransParameters = layeredParametersListType
				.getTransmissionParameters();
		Set<TransmissionParameter> transmissionParameters = Sets.newHashSet();
		for (LayeredParametersType layeredParametersType : LayeredtransParameters) {
			TransmissionParameter transmissionParameter = new TransmissionParameter(
					layeredParametersType);
			transmissionParameters.add(transmissionParameter);
		}
		return transmissionParameters;

	}

	/**
	 * Convert name to naming attribute type.
	 * 
	 * @param name
	 *            the name
	 * @return the naming attribute type
	 */
	public static NamingAttributeType convertNameToNamingAttributeType(
			String name) {
		NamingAttributeType namingAttributeType = new NamingAttributeType();
		List<RelativeDistinguishNameType> rdnList = namingAttributeType
				.getRdn();
		Pattern namePattern = Pattern.compile("(\\w+)=([^::]+)");
		Matcher matcher = namePattern.matcher(name);
		int index = 0;
		while (matcher.find()) {
			RelativeDistinguishNameType rdn = new RelativeDistinguishNameType();

			rdn.setType(matcher.group(1));
			rdn.setValue(matcher.group(2));
			rdnList.add(rdn);
		}

		return namingAttributeType;

	}

	public static String convertNameAndValueListToString(
			List<NameAndStringValueType> nmeStrTypes) {

		StringBuffer stringBuffer = new StringBuffer();

		for (NameAndStringValueType rdnType : nmeStrTypes) {
			stringBuffer.append(rdnType.getName());
			stringBuffer.append("=");
			stringBuffer.append(rdnType.getValue());

			if (nmeStrTypes.indexOf(rdnType) != nmeStrTypes.size() - 1)
				stringBuffer.append("::");

		}

		return stringBuffer.toString();
	}

	public static int convertToBandwidth(
			List<NameAndStringValueType> nmeStrTypes) {
		int bandwidth = 0;

		for (NameAndStringValueType rdnType : nmeStrTypes) {
			if (HuaweiInventoryConstants.CC_Bandwidth.equals(rdnType.getName())) {
				bandwidth = Integer.valueOf(rdnType.getValue());
			}

		}

		return bandwidth;
	}

	public static HashMap<String, String> convertParamerterListToMap(
			byte[] parameterList) {

		String paraString = new String(parameterList);
		HashMap<String, String> parameterMap = Maps.newLinkedHashMap();
		Pattern namePattern = Pattern.compile("(\\w+)=([^::]+)");
		Matcher matcher = namePattern.matcher(paraString);

		while (matcher.find()) {
			parameterMap.put(matcher.group(1), matcher.group(2));

		}

		return parameterMap;
	}

	// created by Jun on 20120321
	public static HashMap<String, String> convertParamerterListToMap(
			String parameterList) {

		String paraString = new String(parameterList);
		HashMap<String, String> parameterMap = Maps.newLinkedHashMap();
		Pattern namePattern = Pattern.compile("(\\w+)=([^::]+)");
		Matcher matcher = namePattern.matcher(paraString);

		while (matcher.find()) {
			parameterMap.put(matcher.group(1), matcher.group(2));

		}

		return parameterMap;

	}

	public static String convertNameAndValueMapToString(HashMap<String, String> nmeStrTypes) {

		StringBuffer stringBuffer = new StringBuffer();

		int index = 0;

		for (Entry<String, String> entry : nmeStrTypes.entrySet()) {
			stringBuffer.append(entry.getKey());
			stringBuffer.append("=");
			stringBuffer.append(entry.getValue());

			if (++index != nmeStrTypes.size() - 1)
				stringBuffer.append("::");

		}

		return stringBuffer.toString();
	}
	
	
	public static String clob2String(java.sql.Clob clobInData) {
		String stringClob = null;
		try {
			long i = 1;
			int clobLength = (int) clobInData.length();
			stringClob = clobInData.getSubString(i, clobLength);
		} catch (Exception e) {
			System.out.println(e);
		}
		return stringClob;
	}

	public static Clob string2Clob(String str) {

		try {
			return new javax.sql.rowset.serial.SerialClob(str.toCharArray());
		} catch (SerialException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
